#!/bin/sh

# must not fail
#set -e 

# debug
set -x 

echo "Starting $0"

EXTRA_DISTNAME=extra_3_9_1.zip
EXTRA_DIRNAME=extra_3_9_1
EXTRA_SOFTLINK=extra
EXTRA_UNPACKCMD=unzip


### copy to "install"

if [ ! -f ${DISTFILES_INSTALL}/${EXTRA_DISTNAME} ] ; then
    echo "copying ${EXTRA_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${EXTRA_DISTNAME}     ${DISTFILES_INSTALL}
fi

### unpack if this version dont exist already
if [ -x ${INSTALL_INTO}/${EXTRA_DIRNAME} ] ;then
    echo "${INSTALL_INTO}/${EXTRA_DIRNAME} exists, skipping"
else

    cd ${INSTALL_INTO}
    ${EXTRA_UNPACKCMD} ${DISTFILES_INSTALL}/${EXTRA_DISTNAME} 
    cd -


### soft links
	rm -f ${EXTRA_SOFTLINK}
	ln -s ${EXTRA_DIRNAME} ${INSTALL_INTO}/${EXTRA_SOFTLINK}

### add to environment
	echo "export EXTRA_HOME=${INSTALL_INTO}/${EXTRA_SOFTLINK}" >> ${INSTALL_INTO}/environment
	export EXTRA_HOME=${INSTALL_INTO}/${EXTRA_SOFTLINK}


### chmod
	chown -R  ${appsrvuser}.${appsrvgrp} ${INSTALL_INTO}/${EXTRA_DIRNAME}

fi

if [ ! -f ${EJBCA_HOME} ] ; then 
	echo "${EJBCA_HOME} not found !! no integration made with ejbca"
else
  if [ ! -x "${EJBCA_HOME}/conf/externalra.properties" ] ; then
	cp ${EJBCA_HOME}/conf/externalra.properties.sample ${EJBCA_HOME}/conf/externalra.properties
	perl -pi -e "s#\#externalra.home=../extra/#externalra.home=${EXTRA_HOME}/gio"  ${EJBCA_HOME}/conf/externalra.properties

  fi
fi

echo "configuring External RA"

# fix ds settings in extra/src/appserver/jboss/ramessage1-ds.xml
echo "Enter ipaddress for external ra1"
read extraip1
perl -pi -e "s#jdbc:mysql://127.0.0.1/messages#jdbc:mysql://${extraip1}/messages#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml

echo "Enter username in mysql for external ra1"
read extrauser1
perl -pi -e "s#user-name>ejbca</user-name#user-name>${extrauser1}</user-name#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml

echo "Enter password in mysql for external ra1"
read extrapass1
perl -pi -e "s#password>ejbca</password#password>${extrapass1}</password#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml

echo "Configure a secondary external RA (y/n)?"
read ra2
if [ "x${ra2}" = "xy" -o "x${ra2}" = "xY" ] ; then
	cp ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml	
	perl -pi -e "s#jndi-name>RAMessage1DS</jndi-name#jndi-name>RAMessage2DS</jndi-name#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml

	cp ${EXTRA_HOME}/src/db/hibernate1.cfg.xml ${EXTRA_HOME}/src/db/hibernate2.cfg.xml
	perl -pi -e "s#hibernate.connection.datasource\">java:/RAMessage1DS</property#hibernate.connection.datasource\">java:/RAMessage2DS</property#gio" ${EXTRA_HOME}/src/db/hibernate2.cfg.xml


	echo "Enter ipaddress for external ra2"
	read extraip2
	perl -pi -e "s#jdbc:mysql://127.0.0.1/messages#jdbc:mysql://${extraip2}/messages#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml

	echo "Enter username in mysql for external ra2"
	read extrauser2
	perl -pi -e "s#user-name>ejbca</user-name#user-name>${extrauser2}</user-name#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml

	echo "Enter password in mysql for external ra2"
	read extrapass2
	perl -pi -e "s#password>ejbca</password#password>${extrapass2}</password#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml


fi


# fix extra/conf/externalra-caservice.properties

perl -pi -e "s/#externalra-caservice.hibernateresource=hibernate1.cfg.xml/externalra-caservice.hibernateresource=hibernate1.cfg.xml/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties

echo "Require encryption (y/n)?"
read extraenc
if [ "x${extraenc}" = "xy" -o "x${extraenc}" = "xY" ] ; then
	perl -pi -e "s/#externalra-caservice.encryption.required=false/externalra-caservice.encryption.required=true/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties
fi

echo "Sign messages (y/n)?"
read extrasign
if [ "x${extrasign}" = "xy" -o "x${extrasign}" = "xY" ] ; then
	perl -pi -e "s/#externalra-caservice.signature.required=false/externalra-caservice.signature.required=true/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties

	echo "Enter the absolute filename of extrakeystore.p12"
	read extrakeystore
	mkdir ${EXTRA_HOME}/keystore
	if [ -f ${extrakeystore} ] ; then
		cp ${extrakeystore} ${EXTRA_HOME}/keystore/extrakeystore.p12
	else
		echo "couldn't find that file ${extrakeystore}, config/copy to ${EXTRA_HOME}/keystore/extrakeystore.p12 manually"
	fi
	
	echo "Enter password for extrakeystore.p12"
	read extrakeystorepass
	perl -pi -e "s/#externalra-caservice.keystore.pwd=foo123/externalra-caservice.keystore.pwd=${extrakeystorepass}/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties

	echo "Enter the CA issuing the RA Certificates"
	read extraca
	perl -pi -e "s/#externalra-caservice.raissuer=AdminCA1/externalra-caservice.raissuer=${extraca}/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties
fi


# deploy keystore 
cd ${EXTRA_HOME}

chown -R jboss.jboss ${INSTALL_INTO}/${EXTRA_DIRNAME}/
    
su -p -s /bin/sh jboss -c "ant deploy-keystore" || echo "build filed"
if [ $? -ne 0 ] ; then
        echo "FAILED: ant deploy-keystore failed"
        echo "config/copy to ${EXTRA_HOME}/keystore/extrakeystore.p12 manually"
fi

echo <<__EOT__
Step 1. Go to ejbca adminweb, Edit Services and add a new service called ExternalRA1, or similar.

Step 2. Edit the service:
Select Worker: Custom worker
Custom Worker Class Path: org.ejbca.extra.caservice.ExtRACAServiceWorker
Custom Worker Properties: optionally you can configure all properties here. See conf/externalra-caservice.properties for information.
Custom Worker Properties: set externalra-caservice.hibernateresource=hibernate1.cfg.xml

__EOT__
if [ "x${ra2}" = "xy" -o "x${ra2}" = "xY" ] ; then
	echo "also configure a secondary external RA called ExternalRA2"
	echo "in Custom Worker Properties: set externalra-caservice.hibernateresource=hibernate2.cfg.xml"
fi
echo "End of $0"


